/*
 *  Copyright 2011 Yuriy Dunko.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client;

import java.util.Map;
import java.util.Properties;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import org.opu.pc.ui.client.actions.AbstractAction;
import org.opu.pc.ui.client.i18n.MenuI18n;
import org.opu.pc.ui.util.Logger;
import org.opu.pc.ui.util.ResourceHandle;

/**
 * @author Yuriy Dunko
 */
public class MainMenuBar extends JMenuBar{
	
    private static final long serialVersionUID = 2727546511444483873L;
	
    private static final String MAIN_PROP = "MenuBar";
    private static final String MENU_PATH = "/org/opu/pc/ui/resource/Menu.properties";
//    private static final String L_A_F = "Laf";
    
    private MenuI18n i18n;
    private Logger logger = Logger.create(this.getClass());
    
    @SuppressWarnings("unused")
    private MainView mainView;

    public MainMenuBar(MainView mainFrame) {
        this.mainView = mainFrame;
        this.i18n = new MenuI18n();
    }

    protected void initMenuBar(Map<String, AbstractAction> actions){
        this.initMenuBar(actions, ResourceHandle.loadProperties(null, MENU_PATH));
    }

    protected void initMenuBar(Map<String, AbstractAction> actions, Properties data){
        String property = data.getProperty(MAIN_PROP);
        logger.debug(this, "Load menues: " + property);
        String[] split = property.split(";");
        for (int i = 0; i < split.length; i++) {
            String string = split[i].trim();
            logger.debug(this, "Process: " + string);
            //if it`s menu
            if (string.charAt(0) == '%'){
                String name = string.substring(1);
                add(getMenu(data, MAIN_PROP + "." + name, name, actions));
            }
            //it`s menu item (it`s bad if item on menubar)
            else {
                add(getMenuItem(string, actions));
            }
        }
    }

    private JMenu getMenu(Properties data, String property, String name, 
            Map<String, AbstractAction> actions) {
        logger.debug(this, "Load menu: " + name + " [" + property + "]");
//        if (L_A_F.equals(name)){
//            return LafAction.getLaFMenu(mainView, i18n.getMenuText(name));
//        }

        JMenu menu = new JMenu(i18n.getMenuText(name));
        String propertyData = data.getProperty(property);
        String[] split = propertyData.split(";");
        for (int i = 0; i < split.length; i++) {
            String string = split[i].trim();
            
            if (string == null || string.isEmpty()){
                continue;
            }
            
            //if it`s menu
            if (string.charAt(0) == '%'){
                String _name = string.substring(1);
                menu.add(getMenu(data, MAIN_PROP + "." + _name, _name, actions));
            }
            //if it`s separator
            else if(string.charAt(0) == '-') {
                menu.addSeparator();
            }
            //it`s menu item (it`s bad if item on menubar)
            else {
                menu.add(getMenuItem(string, actions));
            }
        }
        return menu;
    }

    private JMenuItem getMenuItem(String name,
            Map<String, AbstractAction> actions){
        logger.debug(this, "Load menu item: " + name);
        return new JMenuItem(actions.get(name));
    }

}
